package cn.edu.xmist.expression;


import cn.edu.xmist.conduct.*;
import cn.edu.xmist.constant.ConfigConstant;
import cn.edu.xmist.utils.ActionUtil;
import cn.edu.xmist.utils.PictureUtil;

/**
 * 敌方子弹
 */
public class EnemyBullet extends AbstractPicture implements Moveable, Attackable, Destroyable, Hitable {

	private int speed = 8;
	private Direction direction;
	public EnemyBullet(EnemyTank enemyTank) {
		int[] size = PictureUtil.getImgSize(ConfigConstant.BULLET_DOWN);
		this.width = size[0];
		this.height = size[1];
		
		this.direction = enemyTank.getDircetion();
		
		switch (direction) {
		case UP:
			this.x = enemyTank.getX() + (enemyTank.getWidth()/2 - this.width/2);
			this.y = enemyTank.getY() - this.height / 2;
			break;
			
		case DOWN:
			this.x = enemyTank.getX() + (enemyTank.getWidth()/2 - this.width/2);
			this.y = enemyTank.getY() + enemyTank.getHeight() - this.height / 2;
			break;
			
		case LEFT:
			this.x = enemyTank.getX() - this.width / 2;
			this.y = enemyTank.getY() + (enemyTank.getHeight() / 2 - this.height / 2);
			break;
			
		case RIGHT:
			this.x = enemyTank.getX() + enemyTank.getWidth() - this.width / 2;
			this.y = enemyTank.getY() + ((enemyTank.getHeight() - this.height) / 2);
			break;

		default:
			break;
		}
		
		// 发射子弹声音
		//Config.play(Config.FIRE);
	}

	@Override
	protected void draw() {
		switch (direction) {
		case UP:
			y -= speed;
			PictureUtil.draw(ConfigConstant.BULLET_UP, x, y);
			break;
			
		case DOWN:
			y += speed;
			PictureUtil.draw(ConfigConstant.BULLET_DOWN, x, y);
			break;
			
		case LEFT:
			x -= speed;
			PictureUtil.draw(ConfigConstant.BULLET_LEFT, x, y);
			break;
			
		case RIGHT:
			x += speed;
			PictureUtil.draw(ConfigConstant.BULLET_RIGHT, x, y);
			break;

		default:
			break;
		}

	}

	@Override
	public boolean moveHit(Blockable bp2) {
		AbstractPicture bp = (AbstractPicture) bp2;
		int x1 = bp.x;
		int y1 = bp.y;
		int w1 = bp.width;
		int h1 = bp.height;
		
		int x2 = this.x;
		int y2 = this.y;
		int w2 = this.width;
		int h2 = this.height;
		
		return ActionUtil.isCollsionWithRect(x1, y1, w1, h1, x2, y2, w2, h2);
	}

	@Override
	public boolean checkHit(Hitable bp2) {
		if (bp2 instanceof EnemyTank) {
			return false;
		}
		if (bp2 instanceof EnemyBullet) {
			return false;
		}
		AbstractPicture bp = (AbstractPicture) bp2;
		int x1 = bp.x;
		int y1 = bp.y;
		int w1 = bp.width;
		int h1 = bp.height;
		
		int x2 = this.x;
		int y2 = this.y;
		int w2 = this.width;
		int h2 = this.height;
		
		return ActionUtil.isCollsionWithRect(x1, y1, w1, h1, x2, y2, w2, h2);
	}

	@Override
	public boolean isDestroyed() {
		if (x < 0 || y < 0 || x > ConfigConstant.WindowConstant.WIDTH || y > ConfigConstant.WindowConstant.HEIGHT) {
			return true;
		}
		return false;
	}
	
	@Override
	public Blast showBlast() {
		Blast blast = new Blast(this, false);
		return blast;
	}
}
